home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / extract / chario.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  3.6 KB  |  195 lines

  1. /*
  2.      extract - A network log processor
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. chario.c - 03/20/93
  8.  
  9. */
  10. #include <sys/types.h>
  11. #include <stdio.h>
  12. #include <sys/stat.h>
  13. #include <string.h>
  14. #include <malloc.h>
  15. #include "stdunix.h"
  16.  
  17. #include "chario.h"
  18.  
  19. struct file_str {
  20.      struct file_str *next;
  21.      dev_t dev;
  22.      ino_t inode;
  23.      FILE *fp; /* NULL for internal buffer pointed to by buffer */
  24.      int linenum;
  25.      char *filename;
  26.      unsigned char *buffer;
  27.      int bufptr;
  28. };
  29.  
  30. static struct file_str *current = (struct file_str *)0;
  31. static struct file_str *tail = (struct file_str *)0;
  32.  
  33. int __lastchar;
  34. int __pushed = 0;
  35.  
  36. int
  37. fetchar(void)
  38. {
  39.      int c = EOF;
  40.  
  41.      if(__pushed){
  42.       c = __lastchar;
  43.       __pushed = 0;
  44.       return c;
  45.      }
  46.      else do {
  47.       struct file_str *old = current;
  48.       if(!current)
  49.            return EOF;
  50.       else if(!current->fp){
  51.            if(current->buffer[current->bufptr])
  52.             c = current->buffer[current->bufptr++];
  53.       }
  54.       else if(current->fp){
  55.            c = getc(current->fp);
  56.       }
  57.  
  58.       if(c == EOF){
  59.            if(current->fp){
  60.             if(current->filename)
  61.              free(current->filename);
  62.             fclose(current->fp);
  63.            }
  64.            current = current->next;
  65.            free(old);
  66.            if(!current){
  67.             tail = 0;
  68.             break;
  69.            }
  70.       }
  71.      } while(c == EOF);
  72.  
  73.      if(c == '\n')
  74.       current->linenum++;
  75.      return c;
  76. }
  77.  
  78. int
  79. getlinenum(void)
  80. {
  81.      if(current)
  82.       return current->linenum;
  83.      else
  84.       return -1;
  85. }
  86.  
  87. char *
  88. getfilename(void)
  89. {
  90.      if(current)
  91.       return current->filename;
  92.      else
  93.       return "*last*";
  94. }
  95.  
  96. int
  97. addmembuf(char *buffer, char *name)
  98. {
  99.      struct file_str *fs;
  100.  
  101.      fs = (struct file_str *)malloc(sizeof(struct file_str));
  102.      fs->linenum = 1;
  103.      fs->buffer = (unsigned char *)buffer;
  104.      fs->bufptr = 0;
  105.      fs->fp = (FILE *)0;
  106.      fs->filename = name;
  107.      fs->next = (struct file_str *)0;
  108.      if(tail)
  109.       tail->next = fs;
  110.      tail = fs;
  111.      if(!current)
  112.       current = fs;
  113.      return NOERR;
  114. }
  115.  
  116. int
  117. includemembuf(char *buffer, char *name)
  118. {
  119.      struct file_str *fs;
  120.  
  121.      fs = (struct file_str *)malloc(sizeof(struct file_str));
  122.      fs->linenum = 1;
  123.      fs->buffer = (unsigned char *)buffer;
  124.      fs->bufptr = 0;
  125.      fs->fp = (FILE *)0;
  126.      fs->filename = name;
  127.      fs->next = current;
  128.      if(!tail)
  129.       tail = fs;
  130.      current = fs;
  131.      return NOERR;
  132. }
  133.  
  134. int
  135. includefile(char *filename)
  136. {
  137.      struct stat statbuf;
  138.      struct file_str *rove, *fs;
  139.  
  140.      if(stat(filename, &statbuf) == -1){
  141.       return FILEERR;
  142.      }
  143.      for(rove=current;rove;rove=rove->next)
  144.       if(rove->dev == statbuf.st_dev &&
  145.          rove->inode == statbuf.st_ino)
  146.            return RECURSE;
  147.      fs = (struct file_str *)malloc(sizeof(struct file_str));
  148.      if(!(fs->fp = fopen(filename, "r"))){
  149.       free(fs);
  150.       return FILEERR;
  151.      }
  152.      else {
  153.       fs->dev = statbuf.st_dev;
  154.       fs->inode = statbuf.st_ino;
  155.       fs->linenum = 1;
  156.       fs->filename = strdup(filename);
  157.       fs->buffer = 0;
  158.       fs->next = current;
  159.       if(!tail)
  160.            tail = fs;
  161.       current = fs;
  162.      }
  163.      return NOERR;
  164. }
  165.  
  166. int
  167. addfile(char *filename)
  168. {
  169.      struct stat statbuf;
  170.      struct file_str *fs;
  171.  
  172.      if(stat(filename, &statbuf) == -1){
  173.       return FILEERR;
  174.      }
  175.      fs = (struct file_str *)malloc(sizeof(struct file_str));
  176.      if(!(fs->fp = fopen(filename, "r"))){
  177.       free(fs);
  178.       return FILEERR;
  179.      }
  180.      else {
  181.       fs->dev = statbuf.st_dev;
  182.       fs->inode = statbuf.st_ino;
  183.       fs->linenum = 1;
  184.       fs->filename = strdup(filename);
  185.       fs->buffer = 0;
  186.       fs->next = 0;
  187.       if(tail)
  188.            tail->next = fs;
  189.       tail = fs;
  190.       if(!current)
  191.            current = fs;
  192.      }
  193.      return NOERR;
  194. }
  195.